<template>
  <a-button
    :disabled="disabled"
    class="mr-8px"
    :type="buttonType"
    :size="buttonSize"
    @click="releaseKgtStat"
  >
    {{ props.zpDataFlag ? '正评' : '试评' }}客观题计分
  </a-button>
</template>

<script lang="ts" setup>
import { h } from 'vue'
import { ScoreComputeAndKgtApply } from '/@/api/examinationAudit'
import { useMessage } from '/@/hooks/web/useMessage'
const { createConfirm, createMessage } = useMessage()

const props = defineProps({
  record: { type: Object, default: () => {} },
  disabled: { type: Boolean, default: false },
  zpDataFlag: { type: Boolean, default: false },
  buttonType: { type: String, default: 'primary' },
  buttonSize: { type: String, default: 'default' },
  spinning: { type: Boolean, default: false },
})

const emit = defineEmits(['update:spinning', 'handleClick'])

// 客观题统计计算
const releaseKgtStat = async () => {
  createConfirm({
    iconType: 'warning',
    title: () => h('span', '温馨提醒'),
    content: () =>
      h(
        'span',
        `确认要对${props.zpDataFlag ? '正评' : '试评'}${
          props.record?.kmdm
            ? `科目'${props.record.kmdm}(${props.record.kmmc || props.record.kmdm})'`
            : "'全科目'"
        } 进行客观题得分统吗？`
      ),
    onOk: () => {
      emit('update:spinning', true)
      ScoreComputeAndKgtApply({
        kmdm: props.record?.kmdm,
        zpDataFlag: props.zpDataFlag,
      })
        .then(({ code, message }) => {
          if (code == 200) {
            createMessage.info('科目客观题统计计算成功!')
            emit('handleClick')
          } else {
            createMessage.error(message)
          }
        })
        .finally(() => {
          emit('update:spinning', false)
        })
    },
  })
}
</script>
